package com.bookbuf.cardflipanimation;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;

public class CardFlipActivity extends Activity implements
		FragmentManager.OnBackStackChangedListener {

	/**
	 * Whether or not we're showing the back of the card (otherwise showing the
	 * front).
	 */
	private boolean mShowingBack = false;

	@Override
	protected void onCreate (Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView (R.layout.activity_card_flip);

		if (savedInstanceState == null) {
			// If there is no saved instance state, add a fragment representing
			// the
			// front of the card to this activity. If there is saved instance
			// state,
			// this fragment will have already been added to the activity.
			getFragmentManager ().beginTransaction ()
					.add (R.id.container, new CardFrontFragment ()).commit ();
		} else {
			mShowingBack = (getFragmentManager ().getBackStackEntryCount () > 0);
		}

		// Monitor back stack changes to ensure the action bar shows the
		// appropriate
		// button (either "photo" or "info").
		getFragmentManager ().addOnBackStackChangedListener (this);
	}

	private void flipCard () {
		if (mShowingBack) {
			getFragmentManager ().popBackStack ();
			return;
		}

		// Flip to the back.

		mShowingBack = true;

		// Create and commit a new fragment transaction that adds the fragment
		// for the back of
		// the card, uses custom animations, and is part of the fragment
		// manager's back stack.

		getFragmentManager ().beginTransaction ()

				// Replace the default fragment animations with animator resources
				// representing
				// rotations when switching to the back of the card, as well as animator
				// resources representing rotations when flipping back to the front
				// (e.g. when
				// the system Back button is pressed).
//				.setCustomAnimations (R.animator.card_flip_right_in,
//						R.animator.card_flip_right_out,
//						R.animator.card_flip_left_in,
//						R.animator.card_flip_left_out)

				.setCustomAnimations (R.animator.card_flip_right_in,
						R.animator.card_flip_right_out,
						R.animator.card_flip_left_in,
						R.animator.card_flip_left_out)

				// Replace any fragments currently in the container view with a
				// fragment
				// representing the next page (indicated by the just-incremented
				// currentPage
				// variable).
				.replace (R.id.container, new CardBackFragment ())

				// Add this transaction to the back stack, allowing users to
				// press Back
				// to get to the front of the card.
				.addToBackStack (null)

				// Commit the transaction.
				.commit ();

	}

	@Override
	public void onBackStackChanged () {
		mShowingBack = (getFragmentManager ().getBackStackEntryCount () > 0);
	}

}